package com.greenet.companytemplate.manager.web;

import com.greenet.companytemplate.manager.entity.Menu;
import com.greenet.companytemplate.manager.service.SecondMenuService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/4/26.
 */
@Controller("subMenu")
public class SecondController {
    protected Logger logger= LoggerFactory.getLogger(getClass());
    @Autowired
    private SecondMenuService secondMenuService;

    /**
     * 用户添加菜单，插入到数据库中
     * @param menu
     * @return
     */
    @RequestMapping("/add/second")
    @ResponseBody
    public int addSecond(Menu menu,@RequestParam("parentId") int pid){
        int secondId=0;
        menu.setName(menu.getName().trim());
        try {
            logger.info(menu.getName());
            List<Menu> list = secondMenuService.getMenuByName(menu.getName());
            if (list.size()>0){
                    return -1;
                }
                else {
                    secondId= secondMenuService.insert(menu);
                    logger.info("二级菜单的id："+secondId);
                }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return secondId;
    }

    /**
     * 用户在页面上添加菜单，即可跳转到上传到上传文件的页面上
     * @param pid
     * @param menuId
     * @param name
     * @param thirdId
     * @param model
     * @return
     */
    @RequestMapping("/upload")
    public String upload(@RequestParam("parentId") int pid,
                         @RequestParam("secondId") int menuId,
                         @RequestParam("name") String name,
                         @RequestParam(value = "thirdId",required = false) String thirdId,
                         Model model
                         ){
        model.addAttribute("parentId",pid);
        model.addAttribute("secondId",menuId);
        if(thirdId!=null && !"".equals(thirdId)){
            model.addAttribute("thirdId",thirdId);
        }
        model.addAttribute("name",name.trim());
        return "uploadmain";
    }



}
